const pako = require("pako"); 
const LuckyExcel = require('luckyexcel')
const fs = require('fs')
/**
 * @DESC 导出解压⽅法
 * @param { string } str 
 * @returns
 */
function unzip(str){
let chartData = str 
    .toString()
    .split("")
    .map((i) => i.charCodeAt(0));
      let binData = new Uint8Array(chartData); 
      let data = pako.inflate(binData); 
      return decodeURIComponent(
      String.fromCharCode.apply(null, new Uint16Array(data)) 
  );
};

function checkFileExistsSync(filePath) {
  try {
    fs.accessSync(filePath, fs.constants.F_OK);
    return true;
  } catch (error) {
    return false;
  }
}

function transformExcel(filename) {
  const data = fs.readFileSync(filename);
  return new Promise(async (resolve, reject) => {
    try{
        await LuckyExcel.transformExcelToLucky(data, (exportJson, luckysheetfile) => {
            //console.log('1:',exportJson)
            resolve(exportJson);
          })  
      }
      catch(err){
            reject(err)
      }
  })
}

module.exports={
  unzip,
  transformExcel,
  checkFileExistsSync
}